package com.ruoyi.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.domain.ZhwyRentalcontract;
import com.ruoyi.service.ZhwyDefaultchargesService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.ZhwyChargeitem;
import com.ruoyi.service.IZhwyChargeitemService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收费项目管理Controller
 * 
 * @author junEr
 * @date 2024-05-07
 */
@RestController
@RequestMapping("/hyj/chargeitem")
public class ZhwyChargeitemController extends BaseController
{
    @Autowired
    private IZhwyChargeitemService zhwyChargeitemService;
    @Autowired
    private ZhwyDefaultchargesService defaultchargesService;

    /**
     * 查询收费项目管理列表
     */
    @PreAuthorize("@ss.hasPermi('hyj:chargeitem:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZhwyChargeitem zhwyChargeitem)
    {
        startPage();
        List<ZhwyChargeitem> list = zhwyChargeitemService.selectZhwyChargeitemList(zhwyChargeitem);
        return getDataTable(list);
    }

    /**
     * 导出收费项目管理列表
     */
    @PreAuthorize("@ss.hasPermi('hyj:chargeitem:export')")
    @Log(title = "收费项目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZhwyChargeitem zhwyChargeitem)
    {
        List<ZhwyChargeitem> list = zhwyChargeitemService.selectZhwyChargeitemList(zhwyChargeitem);
        ExcelUtil<ZhwyChargeitem> util = new ExcelUtil<ZhwyChargeitem>(ZhwyChargeitem.class);
        util.exportExcel(response, list, "收费项目管理数据");
    }

    /**
     * 获取收费项目管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('hyj:chargeitem:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(zhwyChargeitemService.selectZhwyChargeitemById(id));
    }

    /**
     * 新增收费项目管理
     */
    @PreAuthorize("@ss.hasPermi('hyj:chargeitem:add')")
    @Log(title = "收费项目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZhwyChargeitem zhwyChargeitem)
    {
        return toAjax(zhwyChargeitemService.insertZhwyChargeitem(zhwyChargeitem));
    }

    /**
     * 修改收费项目管理
     */
    @PreAuthorize("@ss.hasPermi('hyj:chargeitem:edit')")
    @Log(title = "收费项目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZhwyChargeitem zhwyChargeitem)
    {
        return toAjax(zhwyChargeitemService.updateZhwyChargeitem(zhwyChargeitem));
    }

    /**
     * 删除收费项目管理
     */
    @PreAuthorize("@ss.hasPermi('hyj:chargeitem:remove')")
    @Log(title = "收费项目管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(zhwyChargeitemService.deleteZhwyChargeitemByIds(ids));
    }
    /* 关联查询 */
    @GetMapping("/linkList")
    @Anonymous
    public TableDataInfo linkList()
    {
        List<Long> defaultListTemp = defaultchargesService.selectList();
        if(defaultListTemp==null||defaultListTemp.size()==0){
            return null;
        }
        // System.out.println(defaultListTemp);
        Long[] defaultList=new Long[defaultListTemp.size()];
        for (int i = 0; i < defaultListTemp.size(); i++) {
            defaultList[i]= defaultListTemp.get(i);
        }

        // System.out.println(Arrays.toString(defaultList));
        List<ZhwyChargeitem> list = zhwyChargeitemService.selectZhwyChargeitemListAll(defaultList);
        return getDataTable(list);
    }
    @GetMapping("/linkListcs")
    @Anonymous
    public TableDataInfo linkListcs()
    {
        List<Long> defaultListTemp = defaultchargesService.selectListcs();
        if(defaultListTemp==null||defaultListTemp.size()==0){
            return null;
        }
        // System.out.println(defaultListTemp);
        Long[] defaultList=new Long[defaultListTemp.size()];
        for (int i = 0; i < defaultListTemp.size(); i++) {
            defaultList[i]= defaultListTemp.get(i);
        }

        // System.out.println(Arrays.toString(defaultList));
        List<ZhwyChargeitem> list = zhwyChargeitemService.selectZhwyChargeitemListAll(defaultList);
        return getDataTable(list);
    }
    @GetMapping("/notlinkList")
    @Anonymous
    public TableDataInfo notlinkList()
    {
        List<Long> defaultListTemp = defaultchargesService.selectList();
        if(defaultListTemp==null||defaultListTemp.size()==0){
            return null;
        }
        // System.out.println(defaultListTemp);
        Long[] defaultList=new Long[defaultListTemp.size()];

        for (int i = 0; i < defaultListTemp.size(); i++) {
            defaultList[i]= defaultListTemp.get(i);
        }
        List<ZhwyChargeitem> list = zhwyChargeitemService.notselectZhwyChargeitemListAll(defaultList);
        return getDataTable(list);
    }
    @GetMapping("/notlinkListcs")
    @Anonymous
    public TableDataInfo notlinkListcs()
    {
        List<Long> defaultListTemp = defaultchargesService.selectListcs();
        if(defaultListTemp==null||defaultListTemp.size()==0){
            return null;
        }
        // System.out.println(defaultListTemp);
        Long[] defaultList=new Long[defaultListTemp.size()];

        for (int i = 0; i < defaultListTemp.size(); i++) {
            defaultList[i]= defaultListTemp.get(i);
        }
        List<ZhwyChargeitem> list = zhwyChargeitemService.notselectZhwyChargeitemListAll(defaultList);
        return getDataTable(list);
    }

    /**
     * 收费管理
     */
    @Anonymous
    @GetMapping("/getSfgl")
    public AjaxResult getSfgl(String[] time){
        System.out.println(Arrays.toString(time));
        String[] stringArray = {"",""};
        if(time.length > 1){
            StringBuilder sb = new StringBuilder(Arrays.toString(time));
            sb.deleteCharAt(0);
            sb.deleteCharAt(40);
            sb.deleteCharAt(20);
            System.out.println(sb.toString());
            stringArray = sb.toString().split(",");
            for (String s : stringArray) {
                System.out.println(s);
            }
        }
        //获取常规收费
        List<ZhwyChargeitem> cg = zhwyChargeitemService.selectByType("常规收费项（商品）",stringArray[0],stringArray[1]);
        if(!cg.isEmpty()){
            for (int i = 0; i < cg.size()-1; i++) {
                for (int j=i+1;j<cg.size();j++){
                    if(cg.get(i).getName().equals(cg.get(j).getName())){
                        cg.remove(j);
                        j--;
                    }
                }
            }
        }
        System.out.println("==="+cg.toString());
        //获取临时收费
        List<ZhwyChargeitem> ls = zhwyChargeitemService.selectByType("临时收费项",stringArray[0],stringArray[1]);
        if(!ls.isEmpty()){
            for (int i = 0; i < ls.size()-1; i++) {
                for (int j=i+1;j<ls.size();j++){
                    if(ls.get(i).getName().equals(ls.get(j).getName())){
                        ls.remove(j);
                        j--;
                    }
                }
            }
        }
        System.out.println(ls.toString());
        //获取押金收费
        List<ZhwyChargeitem> yj = zhwyChargeitemService.selectByType("押金收费项",stringArray[0],stringArray[1]);
        if(!yj.isEmpty()){
            for (int i = 0; i < yj.size()-1; i++) {
                for (int j=i+1;j<yj.size();j++){
                    if(yj.get(i).getName().equals(yj.get(j).getName())){
                        yj.remove(j);
                        j--;
                    }
                }
            }
        }
        System.out.println(yj.toString());
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("cg",cg);
        map.put("ls",ls);
        map.put("yj",yj);
        return AjaxResult.success(map);
    }
}
